package com.example.demos.service.impl;

import com.example.demos.dto.OrderDetailsDTO;
import com.example.demos.dto.OrderDetailsListDTO;
import com.example.demos.dto.OrdersByGoodsDTO;
import com.example.demos.dto.OrdersDTO;
import com.example.demos.entity.*;
import com.example.demos.mapper.OrdersMapper;
import com.example.demos.service.OrdersService;
import com.example.demos.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public PageBean<List<OrdersDTO>> select(String state, Integer page, Integer rows) {
        Page<OrdersDTO> ordersPage = PageHelper.startPage(page, rows);
        ordersMapper.select(state);
        PageBean<List<OrdersDTO>> pageBean = new PageBean<>(ordersPage.getTotal(), ordersPage.getResult());
        return pageBean;
    }

    @Override
    public PageBean<List<Supplier>> showSupplier(String type) {
        int count = ordersMapper.supplierSum();
        PageBean<List<Supplier>> listPageBean = new PageBean<>(count, ordersMapper.showSupplier(type));
        return listPageBean;
    }

    @Override
    public PageBean<List<OrdersByGoodsDTO>> select1() {
        Page<OrdersByGoodsDTO> ordersPage = PageHelper.startPage(1, 1000);
        ordersMapper.select1();
        PageBean<List<OrdersByGoodsDTO>> pageBean = new PageBean<>(ordersPage.getTotal(), ordersPage.getResult());
        return pageBean;
    }


    @Override
    public int doCheck(Integer id, HttpSession session) {
        Orders orders = new Orders();
        orders.setChecker(session.getAttribute("username").toString());
        orders.setCheckTime(LocalDateTime.now());
        return ordersMapper.doCheck(id, orders);
    }

    @Override
    public int doStart(Integer id, HttpSession session) {
        Orders orders = new Orders();
        orders.setStarter(session.getAttribute("username").toString());
        orders.setStartTime(LocalDateTime.now());
        return ordersMapper.doStart(id, orders);
    }

    @Override
    public int addOrders(OrderDetailsListDTO orderDetailsListDTO, HttpSession session) {
        Orders orders = new Orders();
        orders.setCreateTime(LocalDateTime.now());
        orders.setCreater(session.getAttribute("username").toString());
        orders.setType("0");
        orders.setSupplierUuid(orderDetailsListDTO.getSupplieruuid());
        List<OrderDetailsDTO> orderDetails = orderDetailsListDTO.getOrderDetails();
        double money = 0;
        for (OrderDetailsDTO orderDetail : orderDetails) {
            if (orderDetail.getMoney() == null || orderDetail.getNum() == null) return -1;
            money += orderDetail.getMoney().doubleValue();
        }
        orders.setTotalMoney(new BigDecimal(money));
        orders.setState("0");
        ordersMapper.addOrders(orders);

        orderDetails.stream().forEach(e -> ordersMapper.add(e, orders.getUuid(), orders.getState()));
        return 1;
    }

    @Override
    public int doInStore(BigInteger ordersDetailUuid, BigInteger storeUuid, HttpSession session) {
        //首先查询订单详情所有信息  根据仓库uid 进行入库 入库完成修改订单详情state
        //遍历同一订单下 所有订单详情 确保state是否都为1 如果都为1 修改endtime end 并改状态码为3

        OrderDetail orderDetail = ordersMapper.selectOrderDetail(ordersDetailUuid);
        //判断当前仓库是否存在该商品
        System.out.println(storeUuid+"----------------------");
        System.out.println(orderDetail+"----------------------");
        int count = ordersMapper.selectStoreGoodsSum(storeUuid, orderDetail.getGoodsUuid());
        if (count == 0) {
            ordersMapper.addStoreDetailByCount(storeUuid, orderDetail.getGoodsUuid(), orderDetail.getNum());
        } else if (count == 1) {
            ordersMapper.updateStoreDetailByCount(storeUuid, orderDetail.getGoodsUuid(), orderDetail.getNum());
        }
        ordersMapper.updateOrderDetailState(ordersDetailUuid, LocalDateTime.now(),
                session.getAttribute("username").toString(), orderDetail.getStoreUuid());
        int empId = ordersMapper.selectEmpIdByUsername(session.getAttribute("username").toString());
        ordersMapper.insertStoreOper(empId, LocalDateTime.now(), storeUuid,
                orderDetail.getGoodsUuid(), orderDetail.getNum());
        int orderDetailByOrdersUuid = ordersMapper.selectOrderDetailByOrdersUuid(orderDetail.getOrdersUuid());
        if (orderDetailByOrdersUuid == 0) {
            ordersMapper.updateOrdersEnd(LocalDateTime.now(), session.getAttribute("username").toString(), orderDetail.getOrdersUuid());
        }
        return 1;
    }

}
